package com.test.web.controller;

import com.alibaba.fastjson.JSON;
import com.test.web.service.ILoginService;
import com.test.web.vo.UserVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by caijiao on 2017/3/28.
 */
@Controller
@RequestMapping( "/example" )
public class LoginController  {
    @Resource
    private ILoginService loginService;

    @RequestMapping("/login")
    public String toLogin(){
        return "login";
    }
    @RequestMapping(value="/loginController",method = RequestMethod.POST)
    @ResponseBody
    public String Login(@RequestBody UserVo loginEntity, HttpServletRequest request){

        UserVo userEmail=loginService.checkEmail(loginEntity);
        if(userEmail==null){
            request.setAttribute("noticeMsg","email not register");
            return (String)request.getAttribute("noticeMsg");
        }else{
            UserVo userVo =loginService.login(loginEntity);
            if(userVo==null){
                request.setAttribute("noticeMsg","email or password is error");
                return (String)request.getAttribute("noticeMsg");
            }else {
//                request.setAttribute("userVo",userVo);
                request.getSession().setAttribute("userVo",userVo);
                return "success";
            }

        }
    }
    @RequestMapping("/toMessage")
    public String toMessage(HttpServletRequest request){
        UserVo loginMes = (UserVo) request.getSession().getAttribute("userVo");
        if(loginMes==null){
            return "redirect:/example/login";
        }else {
            return "message";
        }
    }
    @RequestMapping(value="/MessageShow.do",method = RequestMethod.POST)
    @ResponseBody
    public String  MessageShow(HttpServletRequest request,HttpServletResponse response){
//        ModelAndView mav = new ModelAndView("message");
//        UserVo loginMes = (UserVo) request.getSession().getAttribute("userVo");
//        UserVo userLoginVO=loginService.login(loginMes);
//        mav.addObject("userLoginVO", userLoginVO);
//        return mav;
        UserVo loginMes = (UserVo) request.getSession().getAttribute("userVo");
        UserVo userLoginVO=loginService.login(loginMes);
        return JSON.toJSONString(userLoginVO);

    }

    @RequestMapping("/toModify.do")
    public String toModify(HttpServletRequest request){
        UserVo loginMes = (UserVo) request.getSession().getAttribute("userVo");
        if(loginMes==null){
            return "redirect:/example/login";
        }else {
            return "modify";
        }
    }

    @RequestMapping(value="/modify.do",method = RequestMethod.POST)
    @ResponseBody
    public String modify(@RequestBody UserVo modifyVo,HttpServletRequest request){
        UserVo user = (UserVo) request.getSession().getAttribute("userVo");
        if(user==null){
            return "redirect:/example/login";
        }else {
            modifyVo.setUserEmail(user.getUserEmail());
            modifyVo.setPassword(user.getPassword());
            loginService.modifyUserMes(modifyVo);
            request.setAttribute("noticeMsg","success");
            return (String)request.getAttribute("noticeMsg");
        }
    }
    @RequestMapping(value="/toRegister")
    public String toRegister(){
        return "register";
    }
    @RequestMapping(value="/register.do",method = RequestMethod.POST)
    @ResponseBody
    public String register(@RequestBody UserVo registerEntity,HttpServletRequest request){
        UserVo userEmail=loginService.checkEmail(registerEntity);
        if(userEmail!=null){
            request.setAttribute("noticeMsg","email is exist");
        }else{
            loginService.addUser(registerEntity);
            request.setAttribute("noticeMsg","success");
        }
        return (String)request.getAttribute("noticeMsg");
    }

    @RequestMapping(value="/cleanSession.do")
    public String cleanSession(HttpServletRequest request){
        request.getSession().removeAttribute("userVo");
        return "redirect:/example/login";
    }
}
